package com.junxonline.plat.controller.user;

import com.junxonline.common.dao.vo.*;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: UserController
 * @Description: 用户控制器
 */

@RestController
@RequestMapping(value = "/admin/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 获取用户数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getUser")
    public ResultVO getUser(@RequestBody QueryUserVO vo) {

        if (CommonUtils.isEmpty(vo.getUserId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(userService.getUser(vo));
    }

    /**
     * 筛选查询用户数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryUser")
    public PageResultVO queryUser(PageVO page, QueryUserVO vo) {
        return getReturnSuccess(userService.queryUser(page, vo));
    }


    /**
     * 添加用户
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addUser")
    public ResultVO addUser(@RequestBody UserVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getUsername()) || CommonUtils.isEmpty(vo.getNickname()) ||
            CommonUtils.isEmpty(vo.getSex()) || CommonUtils.isEmpty(vo.getStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = userService.addUser(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改用户
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editUser")
    public ResultVO editUser(@RequestBody UserVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getUserId()) || CommonUtils.isEmpty(vo.getUsername()) ||
                CommonUtils.isEmpty(vo.getNickname()) || CommonUtils.isEmpty(vo.getSex()) || CommonUtils.isEmpty(vo.getStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = userService.editUser(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除用户
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delUser")
    public ResultVO delUser(@RequestBody UserIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] userIdsArray = vo.getUserIds();

        boolean successMenu = userService.delUser(userIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 设置角色
     * @param vo
     * @return
     */
    @RequestMapping(value = "/setRole")
    public ResultVO setRole(@RequestBody SetRoleVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getUserIds()) || CommonUtils.isEmpty(vo.getRoleIds())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] userIdsArray = vo.getUserIds();
        Long[] roleIdsArray = vo.getRoleIds();

        boolean success = userService.setRole(userIdsArray, roleIdsArray);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}
